
#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include <math.h>
#include "testHelper.c"

#define NOISY_TEST 1

int checkAdventurer(int testCardPos, struct gameState *testState) {
  int functionError, drawnTreasure, cardDrawn, z;
  int temphand[MAX_HAND];
  struct gameState controlState;

  z = 0;
  drawnTreasure = 0;

  memcpy (&controlState, testState, sizeof(struct gameState));
  functionError = adventurerCard(testState, whoseTurn(testState));

  /* SIMULATE THE CARD EFFECT */
  while(drawnTreasure < 2){
    if(controlState.deckCount[whoseTurn(&controlState)] < 1){
      shuffle(whoseTurn(&controlState), &controlState);
    }
    drawCard(whoseTurn(&controlState), &controlState);
    cardDrawn = controlState.hand[whoseTurn(&controlState)][controlState.handCount[whoseTurn(&controlState)] -\
							    1];
    if(cardDrawn == copper || cardDrawn == silver || cardDrawn == gold){
      drawnTreasure++;
    }
    else{
      temphand[z]=cardDrawn;
      controlState.handCount[whoseTurn(&controlState)]--;
      z++;
    }
  }
  if(z % 10 == 0){
    z = 0;
  }

  while(z-1 >= 0){
    controlState.discard[whoseTurn(&controlState)][controlState.discardCount[whoseTurn(&controlState)]++] = temphand[z-1];
    z = z-1;
  }

  assert(functionError == 0); //no error playing the card                                                      

//test to see if both the states are the same                                                                
  if(memcmp(&controlState, testState, sizeof(struct gameState)) == 0){
    return 0;
  }
  else{
    gameStateDiff(&controlState, testState);
    return 1;
  }
}

int main () {
  int i, j, n, numPlayers, currentPlayer, seed, testCardPos, success, failure;
  int choice1, choice2, choice3;
  int NUM_KINGSOM_CARDS = 10;
  int MIN_PLAYERS = 2;
  int MIN_KINGDOM_CARD = adventurer;
  int KINGDOM_CARD_RANGE = treasure_map - adventurer;
  int k[NUM_KINGDOM_CARDS];
  struct gameState G;

  printf ("***TESTING ADVENTURER***\n");
	
  success = 0;
  failure = 0;
  PutSeed(floor(Random() * MODULUS));

  for (n = 0; n < 2000; n++){ 
    /* Fill the Game State with garbage */
    for (i = 0; i < sizeof(struct gameState); i++) {
      ((char*)&G)[i] = floor(Random() * 256);
    }

    /* Set number of random players */
    numPlayers = floor(Random() * PLAYER_RANGE) + MIN_PLAYERS;
    /* Set random kingdom cards */
    for(i = 0; i < NUM_KINGDOM_CARDS; i++){
      k[i] = floor(Random() * KINGDOM_CARD_RANGE) + MIN_KINGDOM_CARD;
    }
	
    //initialize new game                                                                                      
    seed = floor(Random() * 1000) + 1;
    initializeGame(numPlayers, k, seed, &G);

    /* Play the game until completion */
    while(!isGameOver){
      curPlyr = whoseTurn(&G);

      /* decide on a random card to play */
      cardToPlay = (Random() * G.handCount[curPlyr]);
      choice1, choice2, choice3 = -1;

      /* create options necessary to play card */
      if(G.hand[curPlyr][cardToPlay] == feast){
	choice1 = k[(Random() * 10)];
      }	   

      if(G.hand[curPlyr][cardToPlay] == mine){
	while(G.hand[curPlyr][choice1] != copper && G.hand[curPlyr][choice1] != silver){
	  choice1 = Random * G.handCount[curPlyr];
	}
	choice2 = choice1 + 1;
      }

      if(G.hand[whoseTurn(&G)][cardToPlay] == remodel){
	choice1 = Random * G.handCount[curPlyr];
	choice2 = k[Random() * 10)];
      }	   


      if(G.hand[whoseTurn(&G)][cardToPlay] == baron){
	for(i = 0; i < G.handCount[curPlyr]; i++){
	  if(choice1 == -1 && G.hand[curPlyr][i] < 4 && G.hand[curPlyr][i] > 0){
	    choice1 = G.hand[curPlyr][i];
	  }
	  if(G.hand[curPlyr][i] < choice1){
	    choice1 = G.hand[curPlyr][i];
	  }
	}
      }



    if(checkAdventurer(testCardPos, &G) == 0){
      success++;
    }
    else{
      failure++;
    }
  }
  
  printf ("\n***TESTS COMPLETE***\n***SUMMARY\n***Successful Tests: %d\n***Failed Tests: %d\n\n", success, failure);
  
  return 0;
  
}
